package com.springboot.mybatis.module.admin.login;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.springboot.mybatis.common.PageDTO;
import com.springboot.mybatis.common.ResponseDTO;
import com.springboot.mybatis.common.base.back.BaseBackController;
import com.springboot.mybatis.common.redis.RedisUtil;
import com.springboot.mybatis.common.util.DES3Utils;
import com.springboot.mybatis.common.util.DateUtil;
import com.springboot.mybatis.common.util.MD5Util;
import com.springboot.mybatis.dao.TbChannelMapper;
import com.springboot.mybatis.entity.SysUser;
import com.springboot.mybatis.entity.TbChannel;
import com.springboot.mybatis.module.admin.login.dto.ParamDTO;
import com.springboot.mybatis.module.admin.login.dto.ResultDTO;
import com.springboot.mybatis.module.common.ResponMsgDTO;
import com.springboot.mybatis.service.SysUserService;

@Api("登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController extends BaseBackController{
    
    @Resource
    SysUserService sysUserService;
    @Resource
    RedisUtil redisUtil;
    @Resource
    TbChannelMapper tbChannelMapper;
    
    @ApiOperation(value = "登录接口", notes = "dengluo ", httpMethod = "POST")
    @ApiImplicitParam(name = "user", value = "实体", required = true, dataType = "ParamDTO")
    @RequestMapping(value = "/login")
    public Object login(HttpServletRequest request, @RequestBody ParamDTO user){
        SysUser sysuser = new SysUser();
        sysuser.setLoginName(user.getLoginName());
        List<SysUser> exist = sysUserService.selectAll(sysuser);
        if(exist == null || exist.size() <= 0){
            return ResponMsgDTO.TOKEN_WORONG;
        }
        SysUser users = exist.get(0);
        //用户名+md5pwd,盐值，数据库密码
        Boolean flag = MD5Util.verify(users.getLoginName() + ":" + user.getPassword(), users.getSalt(), users.getPassword());
        if(!flag){
            return ResponMsgDTO.TOKEN_WORONG;
        }
        LocalDateTime now = LocalDateTime.now();
        String expire = DateUtil.Date2Unix2Long(now.plusHours(24)) + "";
        String str = users.getId() + "," + users.getUserName() + "," + expire;
        String token = DES3Utils.INSTANCE.encryptString(str);
        ResultDTO dto = new ResultDTO();
        dto.setExpire(Long.parseLong(expire));
        dto.setToken(token);
        return returnJson(new ResponseDTO<>(dto));
    }
    
    @ApiOperation(value = "登出接口", notes = "", httpMethod = "POST")
    @RequestMapping(value = "/logout")
    public Object logout(HttpServletRequest request){
        List<TbChannel> list = new ArrayList<>();
        for(int i = 0; i<100*100*100; i++){
            TbChannel model = new TbChannel();
            model.setName(i + "");
            list.add(model);
        }
        tbChannelMapper.insert(list);
        return null;
//        Long expire = redisUtil.getExpire("userList", TimeUnit.SECONDS);
//        log.info("过期时间: " + expire);
//        String str = redisUtil.get("userList");
//        if(str == null){
//            log.info("wrong");
//        }
//        List<SysUser> exist = JSONObject.parseObject(str, new TypeReference<List<SysUser>>() {}.getType());
//        log.info("list:" + JSONObject.toJSONString(exist));
//        return returnJson(new ResponseDTO<SysUser>(exist));
    }

}
